﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace TinyERP.Framework.Controls
{
    public delegate void OnChangeThemeHandler(string theme);
    public class ApplicationMenu: MenuStrip, ILocalizable
    {
        public event OnChangeThemeHandler OnChangeTheme;
        private IDictionary<string, string> languageTable;
        private string[] themes = new string[] {
            "Office2007Black"
            ,"Office2007Blue"
            ,"Office2007Silver"
            ,"ProfessionalOffice2003"
            ,"ProfessionalSystem"
            ,"SparkleBlue"
            ,"SparkleOrange"
            ,"SparklePurple"
            };

        public ApplicationMenu()
            : base()
        {
            this.Size = new System.Drawing.Size(850, 24);
            BuildDashboardMenu();
            BuildModuleMenu();
            BuildLanguageMenu();
            BuildAppearanceMenu();
            BuildHelpMenu();
        }

        private void BuildDashboardMenu()
        {
            KToolstripMenuItem dashboard = new KToolstripMenuItem();
            dashboard.Text = AppResourceManager.GetInstance().GetString("Dashboard");
            dashboard.ResourceKey = "Dashboard";

            KToolstripMenuItem switchUser = new KToolstripMenuItem();
            switchUser.Text = AppResourceManager.GetInstance().GetString("SwitchUser");
            switchUser.ResourceKey = "SwitchUser";
            dashboard.DropDownItems.Add(switchUser);

            KToolstripMenuItem editMyProfile = new KToolstripMenuItem();
            editMyProfile.Text = AppResourceManager.GetInstance().GetString("EditMyProfile");
            editMyProfile.ResourceKey = "EditMyProfile";
            dashboard.DropDownItems.Add(editMyProfile);


            KToolstripMenuItem myTask = new KToolstripMenuItem();
            myTask.Text = AppResourceManager.GetInstance().GetString("MyTask");
            myTask.ResourceKey = "MyTask";
            dashboard.DropDownItems.Add(myTask);

            KToolstripMenuItem logout = new KToolstripMenuItem();
            logout.Text = AppResourceManager.GetInstance().GetString("Logout");
            logout.ResourceKey = "Logout";
            dashboard.DropDownItems.Add(logout);

            KToolstripMenuItem exit = new KToolstripMenuItem();
            exit.Text = AppResourceManager.GetInstance().GetString("Exit");
            exit.ResourceKey = "Exit";
            exit.Click += delegate(object sender, EventArgs e)
            {
                Application.Exit();
            };
            dashboard.DropDownItems.Add(exit);

            this.Items.Add(dashboard);
        }

        private void BuildModuleMenu()
        {

        }
        private void BuildLanguageMenu()
        {
            languageTable = new Dictionary<string, string>();
            languageTable.Add("vi-VN", "VietNam");
            languageTable.Add("en-US", "English");
            KToolstripMenuItem language = new KToolstripMenuItem();
            language.Text = AppResourceManager.GetInstance().GetString("Language");
            language.ResourceKey = "Language";
            foreach (string langKey in languageTable.Keys)
            {
                KToolstripMenuItem languageItemMenu = new KToolstripMenuItem();
                languageItemMenu.Text = languageTable[langKey];
                languageItemMenu.Tag = langKey;
                languageItemMenu.Click += delegate(object sender, EventArgs e)
                {
                    string key = (string) ((KToolstripMenuItem)sender).Tag;
                    UIManager.GetInstance().ChangeLanguage(key, this.FindForm());
                };
                language.DropDownItems.Add(languageItemMenu);

            }
            this.Items.Add(language);
        }

        private void BuildAppearanceMenu()
        {
            KToolstripMenuItem appearance = new KToolstripMenuItem();
            appearance.Text = AppResourceManager.GetInstance().GetString("Theme");
            appearance.ResourceKey = "Theme";
            foreach (string themeItem in themes)
            {
                KToolstripMenuItem themeMenu = new KToolstripMenuItem();
                themeMenu.Text = themeItem;
                themeMenu.Tag = themeItem;
                if (themeItem == ApplicationConfiguration.GetInstance().DefaultTheme)
                {
                    themeMenu.Checked = true;
                }
                themeMenu.Click += delegate(object sender, EventArgs e)
                {
                    KToolstripMenuItem selectedThemeMenu = (KToolstripMenuItem)sender;
                    string selectedTheme = selectedThemeMenu.Tag as string;
                    if (OnChangeTheme != null)
                    {
                        OnChangeTheme(selectedTheme);
                    }
                    foreach (KToolstripMenuItem item in appearance.DropDownItems)
                    {
                        item.Checked = false;
                    }
                    selectedThemeMenu.Checked = true;
                };
                appearance.DropDownItems.Add(themeMenu);
            }
            this.Items.Add(appearance);
        }


        private void BuildThemeCollection()
        {

        }
        private void BuildHelpMenu()
        {

        }

        #region ILocalizable Members

        public void UpdateLanguage()
        {
            foreach (ToolStripMenuItem item in this.Items)
            {
                UpdateMenuItemLanguage(item);
            }
        }
        private void UpdateMenuItemLanguage(ToolStripMenuItem item)
        {
            if (item is ILocalizable)
            {
                ((ILocalizable)item).UpdateLanguage();
            }
            if (item.DropDownItems != null)
            {
                foreach (ToolStripMenuItem subItem in item.DropDownItems)
                {
                    UpdateMenuItemLanguage(subItem);
                }
            }
        }
        #endregion
    }
}
